using System;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace GCalSync.Core
{
    class OutlookAppointments : List<Outlook.AppointmentItem>
    {
        /// <summary>
        /// Create collection of appointments with or without google id set
        /// from the specified outlook folder.
        /// </summary>
        /// <param name="apps">Outlook.MAPIFolder which contains appointment items. </param>
        /// <param name="Google">Include only appointments with google ID set (otherwise only those without)</param>
        public OutlookAppointments(Outlook.MAPIFolder appointmentFolder, bool Google)
        {
            foreach (object folderObject in appointmentFolder.Items)
            {
                // Try and cast the folderObject as a Outlook.AppointmentItem.
                Outlook.AppointmentItem appointmentItem = folderObject as Outlook.AppointmentItem;
                
                // If it is a appointment item then see if it is taged or not and add.
                if (appointmentItem != null)
                    if (this.GetCustomField(appointmentItem,"GCalID") != "" && Google)
                        this.Add(appointmentItem);
                    else if (this.GetCustomField(appointmentItem, "GCalID") == "" && !Google)
                        this.Add(appointmentItem);
            }
        }

        /// <summary>
        /// Get by google ID
        /// </summary>
        /// <param name="id">Google ID to look for</param>
        /// <returns></returns>
        public Outlook.AppointmentItem GetById(string id)
        {
            foreach (Outlook.AppointmentItem ap in this)
            {
                Outlook.UserProperty up = ap.UserProperties.Find("GCalID", System.Reflection.Missing.Value);

                if (up != null && up.Value.ToString() == id)
                {
                    return ap;
                }
            }
            return null;
        }

        public void AddCustomField(Outlook.AppointmentItem ai, string fieldName, string fieldValue)
        {
            Outlook.UserProperty up = ai.UserProperties.Find(fieldName, System.Reflection.Missing.Value);
            if (up == null)
            {
                up = ai.UserProperties.Add(fieldName, Outlook.OlUserPropertyType.olText, true,
                    System.Reflection.Missing.Value);
                // and then add the label
                up.Value = fieldValue;
            }
            else
            {
                up.Value = fieldValue;
            }
            ai.Save();
        }

        public  string GetCustomField(Outlook.AppointmentItem ai, string fieldName)
        {
            Outlook.UserProperty up = ai.UserProperties.Find(fieldName, System.Reflection.Missing.Value);
            if (up == null)
                return "";
            else
                return up.Value.ToString();
        }
    }
}
